<?php
function curl($url,$posts="",$cookie=0,$hasHeader=0,$autoref=false)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	if (!empty($posts)) {
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
	}
	curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
	curl_setopt($ch, CURLOPT_HEADER,$hasHeader);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION,$autoref);
	if($cookie==1)
	{
		@curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile.dat");
	}
	@curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile.dat");
	ob_start();
	$result = curl_exec($ch);
	curl_close($ch);
	ob_end_clean();
	return $result;
}
function curl2($url,$post="",$cookie=0,$hasHeader=0,$hasRef=false,&$header="")
{
	$arr=parse_url($url);
	$host=$arr['host'];
	$port=isset($arr['port'])? $arr['port'] : 80;

	$out="";
	if($post=="")
	{
		$out.= "GET ".$arr['path']."?".$arr['query']." HTTP/1.1"."\r\n";
	}else
	{
		$out.= "POST ".$arr['path']."?".$arr['query']." HTTP/1.1"."\r\n";
	}
	$out.= "Host: ".$host."\r\n";
	$out.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10"."\r\n";
	$out.= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"."\r\n";
	$out.= "Accept-Language: en-us,en;q=0.5"."\r\n";
//	$out.= "Accept-Encoding: deflate"."\r\n";

	if($cookie==1)
	{
		$out.=get_Cookie($host);
	}
	$out.= "Keep-Alive: 300"."\r\n";
	$out.= "Connection: close"."\r\n";
	if(!empty($post))
	{
		$out.= "Content-Type: application/x-www-form-urlencoded"."\r\n";
		$out.= "Content-Length: ".strlen($post)."\r\n";
		$out.="\r\n";
		$out.=$post;
	}
	$out.="\r\n\r\n";

	$fp = @fsockopen($host, $port, $errno, $errstr, 5);
	if($fp)
	{
		@fwrite($fp, $out);
		$result="";
		while (!feof($fp)) {
			$result .= fgets($fp, 1024);
		}
		fclose($fp);
	}else
	{
		return false;
	}
	$arr=explode("\r\n\r\n",$result,2);

	$header=$arr[0];
	$body=$arr[1];
	//parse header:
	$lines = preg_split('/[\r\n]+/',trim($header));
	$size = sizeof($lines);
	$header=array();
	$cookies=array();
	$header['status'] = trim($lines[0]);
	for($i=1,$j=1; $i<$size; $i++)
	{
		$line = trim($lines[$i]);
		list($k) = preg_split('/:\s+/',$line);
		if(strtolower($k)=="set-cookie")
		{
			$header[strtolower($k).$j]=preg_replace(';^'.$k.':\s+;','',$line);
			$arr=explode(";",$header[strtolower($k).$j],2);
			$namecookie=substr($arr[0],0,strpos($arr[0],"="));
			$valuecookie=str_replace($namecookie."=","",$arr[0]);
			set_Cookie($host,$namecookie,$valuecookie);
			$cookies[$namecookie]=$valuecookie;
			$j++;
			continue;
		}
		$header[strtolower($k)] = preg_replace(';^'.$k.':\s+;','',$line);
	}

	if(isset($header['location']) && $hasRef==true)
	{
		$header['location']=str_replace("&amp;","&",$header['location']);
		$urlref="http://".$host.$header['location'];
		sleep(1);
		return $this->curl2($urlref,"",1,0);
	}

	if($hasHeader==1)
	{
		return $result;
	}
	return $body;

}

function set_Cookie($host,$name,$value)
{
	$strcookie="";
	$file="cookiefile.dat";
	$data=file_get_contents($file);
	if($data)
	{
		$arr1=explode("(*_*)",$data);
		foreach($arr1 as $i=>$vl)
		{
			$arr2=explode("|*|",$vl);
			if($host==$arr2[0])
			{
				$strcookie=$arr2[1];
			}
		}
		if(!empty($strcookie))
		{
			$match="((".$name."\=)([^\;]+)(\;))";
			$rep=empty($value)? "": $name."=".$value.";";
			$cookienew="";
			if(preg_match($match,$strcookie))
			{
				$cookienew=preg_replace($match,$rep,$strcookie);
			}else if(!empty($value))
			{
				$cookienew=$strcookie.$name."=".$value.";";
			}
			$data=str_replace($host."|*|".$strcookie,$host."|*|".$cookienew,$data);
		}else if(!empty($value))
		{
			$data.=$host."|*|".$name."=".$value.";"."(*_*)";
		}
	}else if(!empty($value))
	{
		$data=$host."|*|".$name."=".$value.";"."(*_*)";
	}
	$f=fopen($file,"w");
	fwrite($f,$data);
	fclose($f);
}
function get_Cookie($host)
{
	$strcookie="";
	$file="cookiefile.dat";
	$data=@file_get_contents($file);
	if($data)
	{
		$arr1=explode("(*_*)",$data);
		foreach($arr1 as $i=>$vl)
		{
			$arr2=explode("|*|",$vl);
			if($host==$arr2[0])
			{
				$strcookie=$arr2[1];
			}
		}
		if(!empty($strcookie))
		{
			$strcookie="Cookie: ".$strcookie."\r\n";
		}
	}
	return $strcookie;
}

?>